/*
 *  Class name: ContactJButton.java
 *  Version: 1.0
 *  Date: 4.6.2014
 */
package cz.muni.fi.pv168.addressbook.gui.components;

import cz.muni.fi.pv168.addressbook.entities.Contact;
import cz.muni.fi.pv168.addressbook.gui.utils.ContactTypeHandler;
import java.util.ResourceBundle;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;

/**
 *
 * @author Michal Ďuriš
 */
public class ContactJButton extends JButton {

    private static final long serialVersionUID = 1L;

    Contact contact = null;

    public ContactJButton() {
        setDefaultSettings();
    }

    public ContactJButton(Action a) {
        super(a);
        setDefaultSettings();
    }

    public ContactJButton(Contact contact) {
        this.contact = contact;
        setDefaultSettings();
    }

    public ContactJButton(Icon icon) {
        super(icon);
        setDefaultSettings();
    }

    public ContactJButton(String text) {
        super(text);
        setDefaultSettings();
    }

    public ContactJButton(String text, Icon icon) {
        super(text, icon);
        setDefaultSettings();
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    @Override
    public String getText() {
        return contact == null ? "" : contact.toString();
    }

    private void setDefaultSettings() {
    }

    public void setPredefinedSettingsWithInternationalization(
            ResourceBundle bundle)
            throws IllegalStateException {

        if (contact == null) {
            throw new IllegalStateException("Contact not set in button !!!");
        }

        setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        setContactIcon();
        setContactToolTipText(bundle);

        setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        setIconTextGap(7);

        setMaximumSize(new java.awt.Dimension(4096, 41));
    }

    private void setContactIcon() throws IllegalStateException {

        if (contact != null) {
            setIcon(ContactTypeHandler.getContactTypImageIcon(contact
                    .getContactType()));
        }
    }

    public void setContactToolTipText(ResourceBundle bundle) throws
            IllegalStateException {

        //@TODO: Change tooltip text for contact buttons
        if (contact != null) {

            setToolTipText(bundle.getString(ContactTypeHandler
                    .getContactTypeInternationalizedTooltipText(contact
                            .getContactType())));
        }
    }
}
